<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import jpushIM from '@/js_sdk/jim.js'
	

	export default {
		watch: {
			isJimInit(n) {
				if (n) {
					const jmAccount = {
						"username": "jiguang_92",
						"password": "123456"
					}

					let data = uni.getStorageSync('jimLoginInfo') || jmAccount
					if (data && data.username) {
						this.$store.dispatch('jimLogin', data);
					}
				}
			}
		},
		computed: {
			
			jimLoginInfo() {
				return uni.getStorageSync('jimLoginInfo')
			},
			isJimInit() {
				return this.$store.getters.isJimInit
			}
		},

		onLaunch: function() {
			
			let self = this
			
			uni.getNetworkType({
				success: function (res) {
					console.log("wdebug---res",res.networkType);
					let data = uni.getStorageSync('jimLoginInfo');
					console.log(data)
					if(res.networkType == 'none') {
						
						uni.showToast({
							title: '网络异常，请稍后再试',
							icon:'none',
							duration:3000
						})
					} else {
						self.$store.dispatch('isConnectAction').then(res => {
							console.log("网络类型-极光IM是否链接", res);
							if(!res) {
								self.$store.dispatch('jimInit')
							}
						})
					}
				}
			});
				
			// 切换网络，可指定关闭哪个网络监听
			let CALLBACK = function(res) {
				if(!res.isConnected) {
					uni.showToast({
						title: '网络异常，请稍后再试',
						icon:'none',
						duration:3000
					})
				} else {
					self.$store.dispatch('isConnectAction').then(res => {
						console.log("切换网络-极光IM是否链接", res);
						
						if(!res) {
							self.$store.dispatch('jimInit')
						}
					})
				}
			}      
			uni.offNetworkStatusChange(CALLBACK)
			uni.onNetworkStatusChange(CALLBACK);		
			
			
			// 使用自定义导航栏
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif
					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		
					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
		},
		methods: {
			...mapMutations(['setLoginStatus']),

		},

		onShow() {
			jpushIM.onDisconnect()
			// this.$store.dispatch('onDisconnectAction');
		},
		onHide: function() {
			this.$store.dispatch('isConnectAction').then(res => {
				if(!res) {
					this.$store.dispatch('jimInit')
				}
			})
			
		}
	}
</script>

<style>
	@import "colorui/main.css";
	@import "colorui/icon.css";

	/*每个页面公共css */
	uni-page-body,
	body {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	}
</style>
